<template>
  <el-card>
    <el-row :gutter="20" class="header">
      <el-col :span="8">
        <el-input placeholder="请输入姓名" clearable v-model="queryForm.name"/>
      </el-col>
      <el-col :span="8">
        <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
        <el-button type="primary" :icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
    </el-row>

    <el-table :data="tableData" style="width: 100%" v-loading="loading">
      <el-table-column prop="name" label="姓名" width="200"/>
      <el-table-column prop="gender" label="性别" width="150">
        <template #default="{ row }">
            {{ row.gender === 1 ? '男' : '女' }}
        </template>
      </el-table-column>
      <el-table-column prop="mobile" label="手机号" width="200"/>
      <el-table-column prop="email" label="邮箱" width="200"/>
      <el-table-column label="操作" width="150">
        <template #default="{ row }">
          <el-button type="danger" size="small" :icon="Delete" @click="handleDelete(row)" />
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="queryForm.pagenum"
      v-model:page-size="queryForm.pageSize"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-card>

  <Dialog
    v-model="dialogVisible"
    :title="dialogTitle"
    :form="currentForm"
    @success="handleSuccess"
  />
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { Search, Plus, Delete } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import Dialog from './components/dialog.vue'
import clientApi from '@/api/client'

const loading = ref(false)
const dialogVisible = ref(false)
const dialogTitle = ref('')
const currentForm = ref({
  user_id: 0,
  name: '',
  mobile: '',
  email: '',
  gender: ''
})
const total = ref(0)
const queryForm = ref({
  name: '',
  pagenum: 1,
  pageSize: 10
})

const tableData = ref([])

// 获取列表数据
const getList = async () => {
  try {
    loading.value = true
    const { data } = await clientApi.getClientList(queryForm.value)
    if (data) {
    if (Array.isArray(data)) {
        tableData.value = data
        total.value = data.length
      } else {
        ElMessage.error('获取客户列表失败')
      }
    } else {
      ElMessage.error('获取客户列表失败')
    }
  } catch (error) {
    console.error('获取列表失败:', error)
    ElMessage.error('获取列表失败')
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  queryForm.value.pagenum = 1
  getList()
}

// 新增
const handleAdd = () => {
  currentForm.value = {
    user_id: 0,
    name: '',
    mobile: '',
    email: '',
    gender: ''
  }
  dialogTitle.value = '新增客户'
  dialogVisible.value = true
}

// 删除
const handleDelete = async (row) => {
  try {
    await ElMessageBox.confirm('确认删除该客户?', '提示', {
      type: 'warning'
    })
    const { data } = await clientApi.deleteClient(row.user_id)
    if (data) {
      ElMessage.success('删除成功')
      getList()
    }
  } catch (error) {
    console.error('删除失败:', error)
    ElMessage.error('删除失败')
  }
}

// 分页
const handleSizeChange = (size) => {
  queryForm.value.pagenum = 1
  queryForm.value.pageSize = size
  getList()
}

const handleCurrentChange = (page) => {
  queryForm.value.pagenum = page
  getList()
}

// 表单提交成功
const handleSuccess = () => {
  getList()
}

onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
.header {
  padding: 16px;
  box-sizing: border-box;
}

:deep(.el-pagination) {
  margin-top: 16px;
  justify-content: center;
}
</style>
